package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.CommonConstantUtil;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.BaseDTO;
import com.financing.wap.dto.FaUserDTO;
import com.financing.wap.dto.ShopperUserInfoDTO;
import com.financing.wap.service.IShopperUserInfoService;
import com.financing.wap.service.IUserService;
import com.financing.wap.dao.IShopperUserInfoDao;
/**
 * shopper_user_info  
 * 店铺基本资料 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Repository("shopperUserInfoService")
public class ShopperUserInfoServiceImpl implements IShopperUserInfoService{

	@Autowired
	private IShopperUserInfoDao shopperUserInfoDao;
	

	@Autowired
	private IUserService userService;

	@Override
	public int insert(ShopperUserInfoDTO shopperUserInfo) throws SQLException {
		return shopperUserInfoDao.insert(shopperUserInfo);
	}

	@Override
	public void update(ShopperUserInfoDTO shopperUserInfo) throws SQLException {
		shopperUserInfoDao.update(shopperUserInfo);
	}

	@Override
	public void updateForce(ShopperUserInfoDTO shopperUserInfo) throws SQLException {
		shopperUserInfoDao.updateForce(shopperUserInfo);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		shopperUserInfoDao.delete(id);
	}
	
	@Override
	public List<ShopperUserInfoDTO> queryList(ShopperUserInfoDTO shopperUserInfo) throws SQLException {
		return shopperUserInfoDao.queryList(shopperUserInfo);
	}

	@Override
	public int queryListCount(ShopperUserInfoDTO shopperUserInfo) throws SQLException {
		return shopperUserInfoDao.queryListCount(shopperUserInfo);
	}

	@Override
	public ShopperUserInfoDTO loadById(Integer id) throws SQLException {
		return shopperUserInfoDao.loadById(id);
	}
 	@Override
	public ShopperUserInfoDTO queryListGetFirst(ShopperUserInfoDTO shopperUserInfo)
			throws SQLException {
		 List<ShopperUserInfoDTO> queryList = queryList(shopperUserInfo);
		 if(queryList==null||queryList.size()==0){
			ShopperUserInfoDTO shopperUserInfoDTO = new ShopperUserInfoDTO();
			 FaUserDTO faUser = userService.getUserById(shopperUserInfo.getUserId());
			 if(faUser != null && faUser.getFaUserWechatDTO() != null && StringUtils.isNotBlank(faUser.getFaUserWechatDTO().getHeadimgurl())){
				 shopperUserInfoDTO.setUserHeadImg(faUser.getFaUserWechatDTO().getHeadimgurl());
			 }else{
				 shopperUserInfoDTO.setUserHeadImg(CommonConstantUtil.DEFUALTIMAGE);
			 }
			return shopperUserInfoDTO;
		 }else{
			 ShopperUserInfoDTO shopperUserInfoDTO = queryList.get(0);
			 FaUserDTO faUser = userService.getUserById(shopperUserInfoDTO.getUserId());
			 if(StringUtils.isNotBlank(shopperUserInfoDTO.getUserHeadImg())&&!shopperUserInfoDTO.getUserHeadImg().matches("/upload/faUser/image/\\d+.png")){
				 //自定义店铺头像优先级最高
			 }else if(faUser != null && faUser.getFaUserWechatDTO() != null && StringUtils.isNotBlank(faUser.getFaUserWechatDTO().getHeadimgurl())){
				//微信头像第二
				 shopperUserInfoDTO.setUserHeadImg(faUser.getFaUserWechatDTO().getHeadimgurl());
			 }else {
				//默认头像
				 shopperUserInfoDTO.setUserHeadImg(CommonConstantUtil.DEFUALTIMAGE);
			 }
			 return shopperUserInfoDTO;
		 }
	}
 	/***
 	 * 同步修改用户店铺信息
 	 * updateType修改类型     1、 修改全部    2、修改头像    3、修改背景图
 	 */
 	
	@Override
	public synchronized void  updateSynchronized(ShopperUserInfoDTO shopperUserInfo,String updateType)
			throws SQLException {
		ShopperUserInfoDTO q = new ShopperUserInfoDTO();
		q.setUserId(shopperUserInfo.getUserId());
		q.setDeleteTag(CommonEnums.DELETE_FLAG_1);
		ShopperUserInfoDTO oldInfo = queryListGetFirst(q);
		//禁止删除该信息
		shopperUserInfo.setDeleteTag(CommonEnums.DELETE_FLAG_1);
		if(oldInfo.getId()==null){
			insert(shopperUserInfo);
		}else{
			//id禁止修改 根据id修改字段属性
			shopperUserInfo.setId(oldInfo.getId());
			if("2".equals(updateType)){
				oldInfo.setUpdateBy(shopperUserInfo.getUpdateBy());
				oldInfo.setUpdateDate(shopperUserInfo.getUpdateDate());
				oldInfo.setUserHeadImg(shopperUserInfo.getUserHeadImg());
				updateForce(oldInfo);
			}else if("3".equals(updateType)){
				oldInfo.setUpdateBy(shopperUserInfo.getUpdateBy());
				oldInfo.setUpdateDate(shopperUserInfo.getUpdateDate());
				oldInfo.setShopBackground(shopperUserInfo.getShopBackground());
				updateForce(oldInfo);
			}else{
				//该接口不修改背景图片
//				shopperUserInfo.setShopBackground(oldInfo.getShopBackground());
				updateForce(shopperUserInfo);
			}
		}
	}
}
